import { getCategoryList,getDiycat} from '../../api/store.js';
import checkNavBar from '../../utils/checkNavBar.js';
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navlist: [],
    productList: [],
    navActive: 0,
    parameter: {
      'navbar': '1',
      'return': '0',
      'title':'产品分类'
    },
    navH:"",
    number:"",
    type:'four',
    category:[],
    scrollLeft: 0,
    selectedTitle: "0",
    selectedId:0,
    childrenId:0,
    secondarySelected:0,
    productList: [
        {
            "id": 106,
            "store_name": "特净皮半生半熟宣纸加棉加皮四尺书画纸生宣书法专用纸",
            "cate_id": "1067,1066",
            "image": "http://qq.gongzuobaogao.cn/82fe4202006031044521025.jpg",
            "sales": "259",
            "price": "198.00",
            "stock": 555,
            "activity": {
                "type": "1",
                "id": 24,
                "time": 1592467200
            },
            "vip_price": "196.02"
        },
        {
            "id": 105,
            "store_name": "古鼎素描纸4K8K16K批发A3A4空白180g素描本水彩纸水粉纸",
            "cate_id": "1067,1065",
            "image": "http://qq.gongzuobaogao.cn/e5544202006031032225766.jpg",
            "sales": "964",
            "price": "198.00",
            "stock": 8168,
            "activity": [],
            "vip_price": "196.02"
        },
        {
            "id": 103,
            "store_name": "手工纸",
            "cate_id": "1067,1066,1065",
            "image": "http://qq.gongzuobaogao.cn/61aed202006031016585980.jpg",
            "sales": "12",
            "price": "111.00",
            "stock": 3330,
            "activity": [],
            "vip_price": "109.89"
        },
        {
            "id": 103,
            "store_name": "手工纸",
            "cate_id": "1067,1066,1065",
            "image": "http://qq.gongzuobaogao.cn/61aed202006031016585980.jpg",
            "sales": "12",
            "price": "111.00",
            "stock": 3330,
            "activity": [],
            "vip_price": "109.89"
        },
        {
            "id": 103,
            "store_name": "手工纸",
            "cate_id": "1067,1066,1065",
            "image": "http://qq.gongzuobaogao.cn/61aed202006031016585980.jpg",
            "sales": "12",
            "price": "111.00",
            "stock": 3330,
            "activity": [],
            "vip_price": "109.89"
        }
    ]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (res) {
    this.getDiycat();
  },
  infoScroll:function(){
    let that = this;
    let len = that.data.category.length;
    that.setData({
      navH: app.globalData.navHeight,
      number: that.data.category[len - 1].children.length
    })
    //设置商品列表高度
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          height: (res.windowHeight) * ( 750/res.windowWidth) - 98 - app.globalData.navHeight
          //res.windowHeight:获取整个窗口高度为px，*2为rpx；98为头部占据的高度；
        })
      },
    });
    var height = 0;
    var hightArr = [];
    for (var i = 0; i < len; i++) { //productList
      //获取元素所在位置
      var query = wx.createSelectorQuery().in(this);
      var idView = "#b" + i;
      query.select(idView).boundingClientRect();
      query.exec(function (res) {
        if(res[0]){
          var top = res[0].top;
          hightArr.push(top);
          that.setData({
            hightArr: hightArr
          });
        }       
      });
    };
  },
  tap: function (e) {
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var categoryId = e.currentTarget.dataset.categoryid;

    if(this.data.type == 'two-3' || this.data.type == 'one-2'){
      this.setData({
        toView: id,
        navActive: index,
        selectedId:categoryId,
        secondarySelected:0,
        selectedTitle:index,
        childrenId:this.data.category[index].children[0].id
      });
    }else{
      this.setData({
        toView: id,
        navActive: index,
        selectedId:categoryId,
        selectedTitle:0,
        childrenId:this.data.category[index].children[0].id
      });
    }
    
  },
  bindtap: function (e) {
    if(this.data.type == 'two-3'){
      this.setData({
        secondarySelected: e.currentTarget.id,
        childrenId:e.currentTarget.dataset.childrenid
      });
    }else{
      this.setData({
        selectedTitle: e.currentTarget.id,
        childrenId:e.currentTarget.dataset.childrenid
      });
    }
  },
  //获取模板
  getDiycat:function(){
    var that = this;
    getDiycat().then(res=>{
      if(res.status == '200' && res.msg == 'ok'){
        var data = res.data.data
        this.setData({
          category:data.category,
          type:data.template,
          selectedId:data.category[0].id,
          childrenId:data.category[0].children[0].id
        })
        that.infoScroll();
      }
    })
  },
  scroll: function (e) {
    if(this.data.type != 'two-4'){
      var scrollTop = e.detail.scrollTop;
      var scrollArr = this.data.hightArr;
      for (var i = 0; i < scrollArr.length; i++) {
        if (scrollTop >= 0 && scrollTop < scrollArr[1] - scrollArr[0]) {
          this.setData({
            navActive: 0,
            lastActive: 0
          })
        } else if (scrollTop >= scrollArr[i] - scrollArr[0] && scrollTop < scrollArr[i + 1] - scrollArr[0]) {
          this.setData({
            navActive: i
          })
        } else if (scrollTop >= scrollArr[scrollArr.length - 1] - scrollArr[0]) {
          this.setData({
            navActive: scrollArr.length - 1
          })
        }
      }
    }
   
  },
  /**
   * 商品详情跳转
   */
  goDetail: function (e) {
    let item = e.currentTarget.dataset.items
   if (item.activity && item.activity.type === "1") {
     wx.navigateTo({
       url: `/pages/activity/goods_seckill_details/index?id=${item.activity.id}&time=${item.activity.time}&status=1`
     });
   } else if (item.activity && item.activity.type === "2") {
    wx.navigateTo({ url:  `/pages/activity/goods_bargain_details/index?id=${item.activity.id}`});
   } else if (item.activity && item.activity.type === "3") {
     wx.navigateTo({
       url: `/pages/activity/goods_combination_details/index?id=${item.activity.id}`
     });
   } else {
     wx.navigateTo({ url: `/pages/goods_details/index?id=${item.id}` });
   }
   },
  searchSubmitValue: function (e) {
    if (e.detail.value.length > 0) 
      wx.navigateTo({ url: '/pages/goods_list/goods_list?searchValue=' + e.detail.value})
    else 
      return app.Tips({ title:'请填写要搜索的产品信息'});
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    checkNavBar(this)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  }
})